<?php
/*
 * @Date: 2021-01-28 19:00:47
 * @名称: 
 * @版本: 0.01
 * @作者: 初雪桜
 * @邮箱: 202184199@qq.com
 * @最后编辑: 初雪桜
 * @LastEditTime: 2021-01-29 13:29:27
 * @FilePath: /Site/app/useradmin/controller/Comics/Get/GetComicsProjectImagesList.php
 */

namespace app\useradmin\controller\Comics\Get;

use app\BaseController;
use think\facade\Db;

class GetComicsProjectImagesList extends BaseController
{
    /**
     * @方法名: 获取图片列表
     * @作者: 初雪桜
     * @信息: 获取某分卷的内容列表
     * @param int $time 时间戳
     * @param String $encryption_bookId    书籍id 加密后
     * @param String $encryption_projectId   章节id 加密后
     * @param String $bookId   书籍id 加密前
     * @param String $projectId   章节id 加密前
     * @param String $hashHmac_bookId  加密后
     * @param String $hashHmac_projectId  加密后
     * @return json
     */
    public function GetImageList($time, $encryption_bookId, $encryption_projectId, $bookId, $projectId, $hashHmac_bookId, $hashHmac_projectId)
    {
        if ( //判断加密验证是否一样
            $time &&
            $encryption_bookId &&
            $encryption_projectId &&
            $bookId &&
            $projectId &&
            $encryption_bookId ==
            $hashHmac_bookId &&
            $encryption_projectId ==
            $hashHmac_projectId
        ) {
            if ($time + 60 > time()) {
                $project_info =  Db::table('book_info_project')
                    ->where([
                        "_id" => $bookId,
                        "chapter_id" => $projectId,
                    ])
                    ->field([
                        "database_table_name" //图片所在数据库
                    ])
                    ->select();

                if (count($project_info) > 1) {
                    return errorJsonReturn("Comics-10000");
                }
                if (count($project_info) < 1) {
                    return errorJsonReturn("Comics-10001");
                }
                //# 判断是否没有分配漫画表 
                $tableName = "";
                if (!$project_info[0]["database_table_name"]) {
                    //#没有分配 分卷表 查询 可以分配的
                    $insertTable = Db::table('setting')
                        ->where([
                            "id" => 1
                        ])
                        ->field("images_insert_table_name")
                        ->find();


                    $tableCount = Db::table("$insertTable[images_insert_table_name]")
                        ->count();

                    if ($tableCount > 48000) {
                        $insertTable["images_insert_table_name"]++;
                        Db::table('setting')
                            ->where([
                                "id" => 1
                            ])
                            ->update([
                                "images_insert_table_name" => $insertTable["images_insert_table_name"],
                            ]);
                    }
                    $tableName = $insertTable["images_insert_table_name"];
                    Db::table('book_info_project')
                        ->where([
                            "_id" => $bookId,
                            "chapter_id" => $projectId,
                        ])
                        ->update([
                            "database_table_name" => $insertTable["images_insert_table_name"], //图片所在数据库
                        ]);
                } else {
                    $tableName = $project_info[0]["database_table_name"];
                }
                $tempComicsImagesList = Db::table($tableName)
                    ->where([
                        "_id" => $bookId,
                        "project_id" => $projectId,
                    ])
                    ->field([
                        "frame_id",
                        "original_name",
                        "image_server_host",
                    ])
                    ->select();
                return successJsonReturn(["list" => $tempComicsImagesList, "service" =>  $tableName]);
            } else {
                return errorJsonReturn("Other-10000");
            }
        } else {
            return errorJsonReturn("Other-10002");
        }
    }
}
